package com.example.controller;

import com.example.common.Result;
import com.example.entity.Alert;
import com.example.service.AlertService;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/alert")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class AlertController {
    private final AlertService alertService;

    public AlertController(AlertService alertService) {
        this.alertService = alertService;
    }

    @GetMapping
    public Result<List<Alert>> getAllAlerts() {
        try {
            List<Alert> alerts = alertService.getAllAlerts();
            if (alerts != null && !alerts.isEmpty()) {
                return Result.success(alerts);
            }
            return Result.success(Collections.emptyList());
        } catch (Exception e) {
            return Result.error("查询报警记录失败：" + e.getMessage());
        }
    }

    @GetMapping("/{siteId}")
    public Result<List<Alert>> getAlertsBySiteId(@PathVariable Integer siteId) {
        try {
            List<Alert> alerts = alertService.getAlertsBySiteId(siteId);
            if (alerts != null && !alerts.isEmpty()) {
                return Result.success(alerts);
            }
            return Result.error("未找到该站点的报警记录");
        } catch (Exception e) {
            return Result.error("查询报警记录失败：" + e.getMessage());
        }
    }
} 